﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.css.transitions {
    import flash.events.*;
    import reprise.css.*;
    import reprise.events.*;
    import com.robertpenner.easing.*;

    public class CSSTransitionsManager {

        protected var m_transitionEasings:Array;
        protected var m_target:EventDispatcher;
        protected var m_adjustedStartTimes:Object;
        protected var m_activeTransitions:Object;
        protected var m_frameDuration:int;
        protected var m_defaultValues:Array;
        protected var m_transitionDelays:Array;
        protected var m_transitionDurations:Array;
        protected var m_transitionProperties:Array;

        protected static const DEFAULT_DURATION:Array = initDefaultDuration();
        protected static const DEFAULT_DELAY:Array = initDefaultDelay();
        protected static const DEFAULT_EASING:Array = [Linear.easeNone];

        protected static var g_transitionShortcuts:Object = {};

        public function CSSTransitionsManager(_arg1:EventDispatcher){
            m_target = _arg1;
            m_adjustedStartTimes = {};
        }
        public function registerAdjustedStartTimeForProperty(_arg1:int, _arg2:String):void{
            m_adjustedStartTimes[_arg2] = _arg1;
        }
        public function processTransitions(_arg1:CSSDeclaration, _arg2:CSSDeclaration, _arg3:CSSPropertiesChangeList, _arg4:int, _arg5:int):CSSDeclaration{
            var transitionPropName:String;
            var transitionDuration:CSSProperty;
            var transitionDelay:CSSProperty;
            var transitionEasing:Function;
            var transition:CSSPropertyTransition;
            var i:int;
            var shortcut:String;
            var cancelEvent:TransitionEvent;
            var defaultValue:*;
            var expansions:Array;
            var defaultValuesForExpansions:CSSParsingResult;
            var name:String;
            var previousRatio:Number;
            var startEvent:TransitionEvent;
            var completeEvent:TransitionEvent;
            var allCompleteEvent:TransitionEvent;
            var oldStyles = _arg1;
            var newStyles = _arg2;
            var changeList = _arg3;
            var frameRate = _arg4;
            var frameTime = _arg5;
            if (((newStyles.hasStyle("transitionFrameDropping")) && ((newStyles.getStyle("transitionFrameDropping").specifiedValue() == "prevent")))){
                m_frameDuration = (1000 / frameRate);
            } else {
                m_frameDuration = 10000;
            };
            if (((newStyles) && (newStyles.hasStyle("RepriseTransitionProperty")))){
                var processProperty:Function = function (_arg1:String, _arg2:String=null, _arg3:CSSProperty=null):void{
                    var _local4:CSSProperty = CSSProperty(((oldStyles) && (oldStyles.getStyle(_arg1))));
                    var _local5:CSSProperty = newStyles.getStyle(_arg1);
                    if (((_arg3) && (((!((_arg3.specifiedValue() == "none"))) || ((_arg1 == "display")))))){
                        if (((_local5) && (!(_local4)))){
                            _local4 = _arg3;
                        } else {
                            if (((_local4) && (!(_local5)))){
                                _local5 = _arg3;
                            };
                        };
                    };
                    if (((((((!(_local4)) || (!(_local5)))) || (_local5.isAuto()))) || ((_local4 == _local5)))){
                        return;
                    };
                    transition = m_activeTransitions[_arg1];
                    if (!transition){
                        transition = new CSSPropertyTransition(_arg1, _arg2);
                        transition.duration = transitionDuration;
                        transition.delay = transitionDelay;
                        transition.easing = transitionEasing;
                        transition.startTime = ((m_adjustedStartTimes[_arg1]) || (frameTime));
                        transition.startValue = _local4;
                        transition.endValue = _local5;
                        m_activeTransitions[_arg1] = transition;
                    } else {
                        if (transition.currentValue.specifiedValue() == _local5.specifiedValue()){
                            delete m_activeTransitions[_arg1];
                        } else {
                            if (transition.endValue != _local5){
                                transition.easing = transitionEasing;
                                transition.updateValues(_local5, transitionDuration, transitionDelay, ((m_adjustedStartTimes[_arg1]) || (frameTime)), m_frameDuration, this);
                            };
                        };
                    };
                };
                m_transitionProperties = newStyles.getStyle("RepriseTransitionProperty").specifiedValue();
                m_transitionDurations = (newStyles.hasStyle("RepriseTransitionDuration")) ? newStyles.getStyle("RepriseTransitionDuration").specifiedValue() : DEFAULT_DURATION;
                m_transitionDelays = (newStyles.hasStyle("RepriseTransitionDelay")) ? newStyles.getStyle("RepriseTransitionDelay").specifiedValue() : DEFAULT_DELAY;
                m_transitionEasings = (newStyles.hasStyle("RepriseTransitionTimingFunction")) ? newStyles.getStyle("RepriseTransitionTimingFunction").specifiedValue() : DEFAULT_EASING;
                m_defaultValues = (((newStyles.hasStyle("RepriseTransitionDefaultValue")) && (newStyles.hasStyle("RepriseTransitionDefaultValue")))) ? newStyles.getStyle("RepriseTransitionDefaultValue").specifiedValue() : [];
                if (m_activeTransitions){
                    for (transitionPropName in m_activeTransitions) {
                        if (m_transitionProperties.indexOf(transitionPropName) == -1){
                            transition = m_activeTransitions[transitionPropName];
                            shortcut = transition.shortcut;
                            if (((!(shortcut)) || ((m_transitionProperties.indexOf(shortcut) == -1)))){
                                delete m_activeTransitions[transitionPropName];
                                cancelEvent = new TransitionEvent(TransitionEvent.TRANSITION_CANCEL);
                                cancelEvent.propertyName = transitionPropName;
                                cancelEvent.elapsedTime = ((frameTime - transition.startTime) - transition.delay.specifiedValue());
                                m_target.dispatchEvent(cancelEvent);
                            };
                        };
                    };
                } else {
                    m_activeTransitions = {};
                };
                i = 0;
                while (i < m_transitionProperties.length) {
                    transitionPropName = m_transitionProperties[i];
                    transitionDuration = ((((m_transitionDurations[i]) || (transitionDuration))) || (DEFAULT_DURATION[0]));
                    transitionDelay = ((((m_transitionDelays[i]) || (transitionDelay))) || (DEFAULT_DELAY[0]));
                    transitionEasing = ((((m_transitionEasings[i]) || (transitionEasing))) || (DEFAULT_EASING[0]));
                    defaultValue = null;
                    if (((m_defaultValues[i]) && (((!((m_defaultValues[i] == "none"))) || ((transitionPropName == "display")))))){
                        defaultValue = CSSPropertyCache.propertyForKeyValue(transitionPropName, m_defaultValues[i], null);
                    };
                    if (g_transitionShortcuts[transitionPropName]){
                        expansions = g_transitionShortcuts[transitionPropName];
                        if (defaultValue){
                            defaultValuesForExpansions = CSSParsingResult(defaultValue);
                            for each (name in expansions) {
                                processProperty(name, transitionPropName, defaultValuesForExpansions.propertyForKey(name));
                            };
                        } else {
                            for each (name in expansions) {
                                processProperty(name, transitionPropName);
                            };
                        };
                    } else {
                        processProperty(transitionPropName, null, CSSProperty(defaultValue));
                    };
                    i = (i + 1);
                };
            };
            if (!m_activeTransitions){
                return (newStyles);
            };
            var styles:CSSDeclaration = newStyles.clone();
            var activeTransitionsCount:int;
            for (transitionPropName in m_activeTransitions) {
                changeList.addChange(transitionPropName);
                transition = m_activeTransitions[transitionPropName];
                previousRatio = transition.currentRatio;
                transition.setValueForTimeInContext(frameTime, m_frameDuration, this);
                if ((((previousRatio == 0)) && (!((transition.currentRatio == 0))))){
                    startEvent = new TransitionEvent(TransitionEvent.TRANSITION_START);
                    startEvent.propertyName = transitionPropName;
                    m_target.dispatchEvent(startEvent);
                };
                styles.setPropertyForKey(transition.currentValue, transitionPropName);
                if (transition.hasCompleted){
                    delete m_activeTransitions[transitionPropName];
                    completeEvent = new TransitionEvent(TransitionEvent.TRANSITION_COMPLETE);
                    completeEvent.propertyName = transitionPropName;
                    completeEvent.elapsedTime = ((frameTime - transition.startTime) - ((transition.delay) ? transition.delay.specifiedValue() : 0));
                    m_target.dispatchEvent(completeEvent);
                } else {
                    activeTransitionsCount = (activeTransitionsCount + 1);
                };
            };
            if (!activeTransitionsCount){
                m_activeTransitions = null;
                allCompleteEvent = new TransitionEvent(TransitionEvent.ALL_TRANSITIONS_COMPLETE);
                m_target.dispatchEvent(allCompleteEvent);
            };
            m_adjustedStartTimes = {};
            return (styles);
        }
        public function hasTransitionForStyle(_arg1:String):Boolean{
            return (((m_transitionProperties) && (!((m_transitionProperties.indexOf(_arg1) == -1)))));
        }
        public function hasActiveTransitionForStyle(_arg1:String):Boolean{
            return (((m_activeTransitions) && (!((m_activeTransitions[_arg1] == null)))));
        }
        public function isActive():Boolean{
            return (!((m_activeTransitions == null)));
        }

        protected static function initDefaultDelay():Array{
            var _local1:CSSProperty = new CSSProperty();
            _local1.setUnit("ms");
            _local1.setSpecifiedValue(0);
            return ([_local1]);
        }
        protected static function initDefaultDuration():Array{
            var _local1:CSSProperty = new CSSProperty();
            _local1.setUnit("ms");
            _local1.setSpecifiedValue(250);
            return ([_local1]);
        }
        public static function registerTransitionShortcut(_arg1:String, _arg2:Array):void{
            g_transitionShortcuts[_arg1] = _arg2;
        }

    }
}//package reprise.css.transitions 
